package com.letoken.platform.app.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.letoken.platform.app.service.AdminFundConfigService;
import com.letoken.platform.app.service.AppFundInfoService;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.repo.admin.dao.AdminFundConfigMapper;
import com.letoken.platform.repo.admin.dao.AppUserSwapRecordMapper;
import com.letoken.platform.repo.admin.po.AdminFundConfig;
import com.letoken.platform.repo.admin.res.user.UpdateDateCapReq;
import com.letoken.platform.repo.app.dao.AppUserWalletMapper;
import com.letoken.platform.repo.app.req.user.FundTransferReq;
import com.letoken.platform.repo.utlis.WebUtils;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Component
@Slf4j
public class UpdateDateJob {

    @Resource
    AppUserSwapRecordMapper appUserSwapRecordMapper;



    @Resource
    AppUserWalletMapper appUserWalletMapper;

    @Autowired
    AppFundInfoService appFundInfoService;

    @Autowired
    AdminFundConfigService adminFundConfigService;

    @XxlJob("UpdateDateLet")
    public  void UpdateDateLet(){
        List<Integer> list =  appUserWalletMapper.getUserIdList();
        AdminFundConfig adminFundConfig = adminFundConfigService.getById(2);
        for (Integer userId:list) {
            try {
                FundTransferReq req = new FundTransferReq();
                req.setAmount(new BigDecimal(appUserWalletMapper.getAvailableBalance(2,userId).intValue()));

                if(adminFundConfig == null || adminFundConfig.getMin().compareTo(req.getAmount())>0){
                    continue;
                }
                req.setUserId(userId);
                req.setFundId(2);
                req.setAdminFundConfig(adminFundConfig);
                checkout(req);
                appFundInfoService.transferInUpdate(req);
            }catch (Exception e){
                log.info("执行异常用户id："+userId);
            }

        }
    }



    private void checkout(FundTransferReq req){
        if ( req.getAmount().compareTo(BigDecimal.ONE) < 0){
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_MALL_SMS_054);
        }
        //Integer balanceId = appBalanceWalletMapper.getIdByUserId(LetokenTokenTypeEnum.LET.getId(),req.getUserId());
        Integer walletId = appUserWalletMapper.checkoutTokenIdAndUserId(LetokenTokenTypeEnum.LET.getId(), req.getUserId());
        if (/*ObjectUtils.isEmpty(balanceId) ||*/ ObjectUtils.isEmpty(walletId)){
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_MALL_SMS_051);
        }
        //req.setBalanceId(balanceId);
        req.setWalletId(walletId);
    }


}
